Skip to content

Conversation

Sahil-4555
Copy link

@Sahil-4555 Sahil-4555 commented Aug 28, 2025

This PR makes the following improvements:

  • Refactors code to use Go’s standard library functions instead of custom implementations for better readability and maintainability.
  • Optimizes fastANDBytes, fastORBytes, and fastXORBytes by using bit shifts, reducing unnecessary operations and improving performance & benchmark by around 50%.

fastXORBytes

goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/common/bitutil
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
             │ changes_bench.txt │         original_bench.txt          │
             │      sec/op       │   sec/op     vs base                │
FastXOR1KB-8         56.64n ± 3%   91.44n ± 3%  +61.43% (p=0.000 n=10)
FastXOR2KB-8         102.4n ± 4%   173.0n ± 1%  +69.03% (p=0.000 n=10)
FastXOR4KB-8         207.8n ± 2%   340.7n ± 2%  +64.00% (p=0.000 n=10)
geomean              106.4n        175.3n       +64.79%

             │ changes_bench.txt │         original_bench.txt          │
             │       B/op        │    B/op     vs base                 │
FastXOR1KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastXOR2KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastXOR4KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

             │ changes_bench.txt │         original_bench.txt          │
             │     allocs/op     │ allocs/op   vs base                 │
FastXOR1KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastXOR2KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastXOR4KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

fastANDBytes

goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/common/bitutil
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
             │ changes_bench.txt │          original_bench.txt          │
             │      sec/op       │    sec/op     vs base                │
FastAND1KB-8         58.76n ± 5%   89.81n ± 10%  +52.86% (p=0.000 n=10)
FastAND2KB-8         108.7n ± 1%   165.3n ±  3%  +52.02% (p=0.000 n=10)
FastAND4KB-8         208.9n ± 2%   324.2n ±  1%  +55.22% (p=0.000 n=10)
geomean              110.1n        168.8n        +53.36%

             │ changes_bench.txt │         original_bench.txt          │
             │       B/op        │    B/op     vs base                 │
FastAND1KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastAND2KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastAND4KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

             │ changes_bench.txt │         original_bench.txt          │
             │     allocs/op     │ allocs/op   vs base                 │
FastAND1KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastAND2KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastAND4KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                        ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

fastORBytes

goos: linux
goarch: amd64
pkg: github.com/ethereum/go-ethereum/common/bitutil
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
            │ changes_bench.txt │         original_bench.txt          │
            │      sec/op       │   sec/op     vs base                │
FastOR1KB-8         55.26n ± 4%   90.00n ± 5%  +62.84% (p=0.000 n=10)
FastOR2KB-8         101.8n ± 1%   164.5n ± 4%  +61.64% (p=0.000 n=10)
FastOR4KB-8         195.9n ± 1%   321.8n ± 3%  +64.24% (p=0.000 n=10)
geomean             103.3n        168.3n       +62.90%

            │ changes_bench.txt │         original_bench.txt          │
            │       B/op        │    B/op     vs base                 │
FastOR1KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastOR2KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastOR4KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                       ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

            │ changes_bench.txt │         original_bench.txt          │
            │     allocs/op     │ allocs/op   vs base                 │
FastOR1KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastOR2KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
FastOR4KB-8        0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10) ¹
geomean                       ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

@Sahil-4555 Sahil-4555 changed the title Common: Refactor to use std lib functions and optimize bitutil operations common: refactor to use std lib functions and optimize bitutil operations Aug 28, 2025
@MariusVanDerWijden
Copy link
Member

We are not really using the bitutil, we vendored it in to use a single function afaict, but I don't think we should start optimizing it, will discuss it in triage

@fjl
Copy link
Contributor

fjl commented Oct 14, 2025

We have decided not to merge this PR.

@fjl fjl closed this Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants